package com.xmqian.app.project.ui.common.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.widget.LinearLayout
import androidx.fragment.app.FragmentManager
import com.example.xmqian.R
import com.example.xmqian.databinding.LayoutTitleFragmentBinding
import com.xmqian.app.project.common.base.BaseMyActivity
import com.xmqian.app.project.ui.common.fragment.MyWebViewFragment
import com.xmqian.app.project.ui.common.fragment.MyWebViewFragment.TitleCallBack

/**
 * Desc： 网页展示
 *
 *
 * Author： xmqian
 * Email:xmqian93@163.com
 * Date: 2019/9/20 11:27
 */
class MyWebViewActivity : BaseMyActivity(), TitleCallBack {
    private var supportFragmentManager: FragmentManager? = null
    private var binding: LayoutTitleFragmentBinding? = null

    override fun getLayoutView(): LinearLayout {
        binding = LayoutTitleFragmentBinding.inflate(layoutInflater)
        return binding!!.root
    }

    override fun initView() {
        requestTranslucentStatusBar(Color.parseColor("#FFFFFF"), true)
        val pageId = intent.getIntExtra(EXTRA_PAGE_ID, 0)
        val type = intent.getIntExtra(EXTRA_TYPE, -1)
        val text = intent.getStringExtra(EXTRA_TEXT)
        val title = intent.getStringExtra(EXTRA_TITLE)
        val myWebViewFragment: MyWebViewFragment =
            MyWebViewFragment.Companion.newInstance(type, pageId, title, text)
        supportFragmentManager = getSupportFragmentManager()
        supportFragmentManager!!.beginTransaction().replace(R.id.fl_content, myWebViewFragment)
            .commit()
    }

    override fun initData() {
    }

    override fun onBackPressed() {
        val fragment = supportFragmentManager!!.findFragmentById(R.id.fl_content)
        if (fragment != null && fragment is MyWebViewFragment) {
            if (fragment.onBackPressed()) {
                return
            }
            super.onBackPressed()
        }
    }

    override fun setTitleListenner(title: String?) {
        setTitle(title)
    }

    companion object {
        private const val EXTRA_TYPE = "mCurrentType"
        private const val EXTRA_PAGE_ID = "pageId"
        private const val EXTRA_TEXT = "text"
        private const val EXTRA_TITLE = "title"

        /**
         * 获取intent，启动网页或打开内容
         */
        fun getIntent(context: Context?, type: Int, pageId: Int): Intent {
            val intent = Intent(context, MyWebViewActivity::class.java)
            intent.putExtra(EXTRA_TYPE, type)
            intent.putExtra(EXTRA_PAGE_ID, pageId)
            return intent
        }

        /**
         * 获取intent，启动网页或打开内容
         *
         * @param context
         * @param title 要显示的标题
         * @param url url、文本/html 片段
         * @return
         */
        fun getIntent(context: Context?, title: String?, url: String?): Intent {
            val intent = Intent(context, MyWebViewActivity::class.java)
            intent.putExtra(EXTRA_TITLE, title)
            intent.putExtra(EXTRA_TEXT, url)
            return intent
        }

        /**
         * 获取intent，打开特定的页面，如：在线支付，用于检测服务器返回的结果（json字符串）
         *
         * @param context
         * @param title 要显示的标题
         * @param url url 地址
         * @param type 本类所定义的常量值，如 TYPE_BANK_PAYMENT
         * @return
         */
        fun getIntent(context: Context?, title: String?, url: String?, type: Int): Intent {
            return getIntent(context, title, url).putExtra(EXTRA_TYPE, type)
        }
    }
}
